/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.communicate;

import com.ssm.changeunit.ChangeLog;
import com.ssm.changeunit.ChangeUnitItem;
import com.ssm.changeunit.persistance.ChangeUnitDTO;
import com.ssm.changeunit.persistance.ChildDTO;
import com.ssm.changeunit.persistance.ParentDTO;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

/**
 *
 * @author 시리우스
 */
public class ChangeUnitClient {
    private String server;
    private XmlRpcClient client;
    
    public ChangeUnitClient(String server) {
        this.server = server;
        
        // create configuration
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        try {
            config.setServerURL(new URL(server));
        } 
        catch (MalformedURLException ex) { }
        config.setEnabledForExtensions(true);
        config.setConnectionTimeout(60 * 1000);
        config.setReplyTimeout(60 * 1000);
        client = new XmlRpcClient();
        
        // set configuration
        client.setConfig(config);
    }
    
    public String getCurrentChangeUnitIdOfBranch(String branchName) {
        try {
            Object[] params = new Object[]{branchName};
            // make the a regular call
            return (String) client.execute("ChangeUnitRPC.getCurrentChangeUnitIdOfBranch", params);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    
    public boolean isChangeUnitExists(String branchName, String changeUnitId) {
        try {
            Object[] params = new Object[]{branchName, changeUnitId};
            // make the a regular call
            return (Boolean) client.execute("ChangeUnitRPC.isChangeUnitExists", params);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        return false;
    }
    
    public ChangeUnitRPCResult getChangeUnitRPCResult(String startChangeUnitId, String endChangeUnitId, String branchName) {
        try {
            Object[] params = new Object[]{startChangeUnitId, endChangeUnitId, branchName};
            // make the a regular call
            return (ChangeUnitRPCResult) client.execute("ChangeUnitRPC.getChangeUnitRPCResult", params);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    
    public void push(ChangeUnitRPCResult changeUnitRPCResult) {
        try {
            Object[] params = new Object[]{changeUnitRPCResult};
            // make the a regular call
            client.execute("ChangeUnitRPC.push", params);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    public byte[] getDatabase() {
        try {
            Object[] params = new Object[]{};
            // make the a regular call
            return (byte[]) client.execute("ChangeUnitRPC.getDatabase", params);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
}
